﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OpenServe.Net.Messaging {
    using System.Threading;
    using OpenServe.Net.Client;
    using OpenServe.Net.Library.Extentions;
    using OpenServe.Net.Server;
    using System.Reflection;
    using System.Threading.Tasks;
    using OpenServe.Net.Library.Interfaces;
    public abstract class BaseHandler : IMsgHandler {
        public dynamic Parent { get; set; }
        public string MessageType { get; set; }
        public BaseHandler(dynamic _parent, string _messageType) { Parent = _parent; MessageType = _messageType; }
        public virtual void HandleMessage(Message msg) {
            if (MessageType == msg.Header.MessageType) {
                var meth = this.GetType().GetMethod(msg.Header.Operation);
                if (meth != null) meth.Invoke(this, new object[] { msg.Clone() });
            }
        }
        public void Send(Message msg) {
            msg.Header.MessageType = this.MessageType;
            Parent.Send(msg);
        }
        public void SendAndWait(Message msg, ManualResetEvent mre) {
            Send(msg); mre.Reset(); mre.WaitOne();
        }

    }
}
